package com.hillky.springboot.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import com.hillky.springboot.common.Result;
import com.hillky.springboot.controller.request.BorrowPageRequest;
import com.hillky.springboot.entity.Borrow;
import com.hillky.springboot.entity.Retur;
import com.hillky.springboot.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/borrow")
@CrossOrigin
public class BorrowController {

    @Autowired
    private BorrowService borrowService;


    @PostMapping("/save")
    public Result save(@RequestBody Borrow borrow) {
        borrowService.save(borrow);
        return Result.success();
    }

    @PutMapping("/")
    public Result update(@RequestBody Borrow borrow) {
        borrowService.update(borrow);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        borrowService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Borrow res = borrowService.getById(id);
        return Result.success(res);
    }

    @GetMapping("/list")
    public Result list() {
        List<Borrow> res = borrowService.list();
        return Result.success(res);
    }

    @GetMapping("/page")
    public Result page(BorrowPageRequest borrowPageRequest) {
        PageInfo<Borrow> page = borrowService.page(borrowPageRequest);
        List<Borrow> borrowList = page.getList();
        for (Borrow borrow : borrowList) {
            LocalDate returnDate = borrow.getReturnDate();
            LocalDate now = LocalDate.now();
            if(now.plus(1, ChronoUnit.DAYS).equals(returnDate)){
                borrow.setNote("即将到期");
            }else if(now.equals(returnDate)){
                borrow.setNote("已到期");
            }else if(now.isAfter(returnDate)){
                borrow.setNote("已过期");
            }else{
                borrow.setNote("正常");
            }
        }
        return Result.success(page);
    }

    @GetMapping("/pageRetur")
    public Result pageRetur(BorrowPageRequest borrowPageRequest) {
        PageInfo<Retur> page = borrowService.pageRetur(borrowPageRequest);
        return Result.success(page);
    }

    @PostMapping("/saveRetur")
    public Result saveRetur(@RequestBody Retur retur) {
        borrowService.saveRetur(retur);
        return Result.success();
    }

    @DeleteMapping("/retur/{id}")
    public Result deleteRetur(@PathVariable Integer id) {
        borrowService.deleteReturById(id);
        return Result.success();
    }

    @GetMapping("/lineCharts/{timeRange}")
    public Result lineCharts(@PathVariable String timeRange) {
        return Result.success(borrowService.getCountByTimeRange(timeRange));
    }
}
